package jmu.edu.yjc.controller;

import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.dto.UserDto;
import jmu.edu.yjc.domain.entity.User;
import jmu.edu.yjc.service.user.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;

@RestController
@RequestMapping("/system/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/list")
    public ResponseResult listUser(Integer pageNum, Integer pageSize,
                                   String userName, String phonenumber,
                                   String status){
        return userService.listUser(pageNum,pageSize,userName,phonenumber,status);
    }
    @PostMapping
    public ResponseResult addUser(@RequestBody UserDto userDto){
        return userService.addUser(userDto);
    }
    @DeleteMapping("/{userId}")
    public ResponseResult deleteUser(@PathVariable("userId") String userId){
        String[] ids = userId.split(",");
        userService.removeByIds(Arrays.asList(ids));
        return ResponseResult.okResult();
    }
    @GetMapping("/{userId}")
    public ResponseResult editUser(@PathVariable("userId") Long userId){
        return userService.editUser(userId);
    }
    @PutMapping
    public ResponseResult doEditUser(@RequestBody UserDto userDto){
        return userService.doEditUser(userDto);
    }
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody User user){
        userService.updateById(user);
        return ResponseResult.okResult();
    }
    @GetMapping("/total")
    public ResponseResult getTotal() {
        return ResponseResult.okResult(userService.count());
    }
    @GetMapping("/queryUser")
    public ResponseResult queryUser(String query) {
        return userService.queryUser(query);
    }
}
